package pers.vic.test.thymeleaf;

import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * 描述: select 下拉框 的值是枚举的时候 <select vic:enum="xxx.xxx.XxxEnum"></>
 * @author Vic.xu
 * @date 2022-02-11 9:38
 */
public class EnumElementTagProcessor extends AbstractAttributeTagProcessor {

    public static final String ATTRIBUTE_NAME = "enum";

    private static final String DELIMITER = ",";
    static int PRECEDENCE = 300;

    public EnumElementTagProcessor(String dialectPrefix) {
        super(
                // 处理thymeleaf 的模型
                TemplateMode.HTML,
                // 标签前缀名 相当于th:if中的th
                dialectPrefix,
                // No tag name: match any tag name
                null,
                // No prefix to be applied to tag name
                false,
                // 标签前缀的 属性 例如：<sec:permission="">
                ATTRIBUTE_NAME,
                // Apply dialect prefix to attribute name
                true,
                // Precedence (inside dialect's precedence)
                PRECEDENCE,
                // Remove the matched attribute afterwards
                true);
    }


    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {
        final IEngineConfiguration configuration = context.getConfiguration();
        /*
         * 获得Thymeleaf标准表达式解析器
         */
        final IStandardExpressionParser parser =
                StandardExpressions.getExpressionParser(configuration);

        /*
         * 将属性值解析为Thymeleaf标准表达式
         */
        final IStandardExpression expression = parser.parseExpression(context, attributeValue);
        tag.getTemplateMode();
        //标签名
        String elementCompleteName = tag.getElementCompleteName();
        System.out.println("elementCompleteName = " + elementCompleteName);
        String pcode = String.valueOf(expression.execute(context));
        IModelFactory modelFactory = context.getModelFactory();
        //modelFactory.creelementtag

        IModel model = modelFactory.createModel();
        model.add(modelFactory.createOpenElementTag("option value=\"zhangsan\""));
        model.add(modelFactory.createText("zhangsan"));
        model.add(modelFactory.createCloseElementTag("option"));
        structureHandler.insertImmediatelyAfter(model, true);
    }

}
